package com.sskj.wallet.ui.fragment;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.bean.TransferRecordBean;
import com.sskj.wallet.presenter.CoinRecordFragmentPresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

@Route(path = RConfig.WALLET_FRAGMENT_COIN_RECORD)
public class CoinRecordFragment extends BaseFragment<CoinRecordFragmentPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;

    @Autowired
    String address;
    @Autowired
    String type;
    @Autowired
    String state;
    @Autowired
    String code;

    @Override
    protected int getLayoutId() {
        return R.layout.wallet_fragment_coin_record;
    }

    @Override
    public CoinRecordFragmentPresenter getPresenter() {
        return new CoinRecordFragmentPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        HashMap<String, Integer> statusMap = new HashMap<>();
        statusMap.put("1", R.mipmap.wallet_icon_transfer_down);
        statusMap.put("2", R.mipmap.wallet_icon_tranfer_up);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
        SlimAdapter slimAdapter = SlimAdapter.create().register(R.layout.wallet_recy_item_coin_record, new SlimInjector<TransferRecordBean>() {
            @Override
            public void onInject(TransferRecordBean data, IViewInjector injector, List payloads) {

                injector
                        .image(R.id.ivStatus, statusMap.get(data.getType()))
                        .text(R.id.tvHash, data.getHash())
                        .text(R.id.tvNum, String.format("%s%s%s", data.getType().equals("1") ? "-" : "+", data.getPrice(), data.getPtype()))
                        .text(R.id.tvTime, data.getAddtime())
                        .text(R.id.tvDesc, data.getMemo());
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<TransferRecordBean> dataSource = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<TransferRecordBean>)
                page -> mPresenter.getData(address, code, type, state, page + "", "20"), 20);
        MVCCoolHelper<List<TransferRecordBean>> mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener((recyclerView1, i, view) -> {
                    ARouter.getInstance().build(RConfig.WALLET_RECORD_INFO)
                            .withSerializable(Constans.DATA, (TransferRecordBean) slimAdapter.getDataItem(i))
                            .withString(Constans.HASH, ((TransferRecordBean) slimAdapter.getDataItem(i)).getHash())
                            .withString(Constans.TYPE, ((TransferRecordBean) slimAdapter.getDataItem(i)).getType())
                            .navigation();
                });
    }
}
